package com.ctrlcar.giftrecord.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctrlcar.giftrecord.dto.ReceiveGiftDTO;
import com.ctrlcar.giftrecord.mapper.ReceiveGiftMapper;
import com.ctrlcar.giftrecord.pojo.ReceiveGift;

import com.ctrlcar.giftrecord.pojo.Relative;
import com.ctrlcar.giftrecord.pojo.model.Result;
import com.ctrlcar.giftrecord.service.GiftBookService;
import com.ctrlcar.giftrecord.service.ReceiveGiftService;
import com.ctrlcar.giftrecord.service.RelativeService;
import com.ctrlcar.giftrecord.vo.ReceiveGiftVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author
* @description 针对表【receive_gift】的数据库操作Service实现
* @createDate 2025-03-26 16:43:36
*/
@Service
public class ReceiveGiftServiceImpl extends ServiceImpl<ReceiveGiftMapper, ReceiveGift>
    implements ReceiveGiftService {

    @Resource
    private GiftBookService giftBookService;
    @Resource
    private RelativeService relativeService;


//    根据礼簿id获取礼物记录list
    @Override
    public List<ReceiveGiftVo> getReceiveGiftByBookId(Integer bookId,String sel) {
        //联合查询礼簿和亲朋，根据id联合查询亲朋名称
        LambdaQueryWrapper<ReceiveGift> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReceiveGift::getBookId, bookId);
        List<ReceiveGift> list = list(queryWrapper);
        List<ReceiveGiftVo> list1 = new ArrayList<>();
        for (ReceiveGift receiveGift : list){
            Relative relative = relativeService.getById(receiveGift.getRelativeId());
            ReceiveGiftVo receiveGiftVo = BeanUtil.copyProperties(receiveGift, ReceiveGiftVo.class);
            receiveGiftVo.setRelativeName(relative.getName());
            if (sel != null && !sel.isEmpty()) {
                if (receiveGiftVo.getRelativeName().contains(sel)) {
                    list1.add(receiveGiftVo);
                }
            }else {
                list1.add(receiveGiftVo);
            }
        }
        return list1;
    }

    /**
     * 1.0有这个用户但是没有输入地址(返回请输入地址)
     * 1.1有这个用户但是地址不一样(新建+新建收礼表)
     * 1.2有这个用户地址一样(新建收礼表)
     *
     * @param receiveGiftDTO
     * @return
     */
    @Transactional
    @Override
    public Result addReceiveGift(ReceiveGiftDTO receiveGiftDTO) {
        //1.查询用户的亲朋记录list
        List<Relative> relativeList = relativeService.list(
                new LambdaQueryWrapper<Relative>()
                        .eq(Relative::getUserId, receiveGiftDTO.getUserId())
                        .eq(Relative::getName, receiveGiftDTO.getReceiveGiftName()));
        if (relativeList.isEmpty()) {
            // 1.1 list<1(亲朋表没有同名用户) =》创建用户+添加收礼记录
            Relative relative = new Relative();
            relative.setUserId(receiveGiftDTO.getUserId());
            relative.setName(receiveGiftDTO.getReceiveGiftName());
            relative.setAddress(receiveGiftDTO.getAddress());
            relative.setRemark("");
            relativeService.save(relative);
            return createReceiveGiftRecord(receiveGiftDTO, relative.getRelativeId());
        }
        else if (relativeList.size() >= 1) {
            for (Relative relative : relativeList) {
                if (receiveGiftDTO.getAddress() == null || receiveGiftDTO.getAddress().isEmpty()) {
                    return Result.error("有存在多个用户，请输入地址");
                } else {
                    // 1.4 list>1(亲朋表有多个用户 有填写地址 地址一样) =》创建收礼记录
                    if (relative.getAddress().equals(receiveGiftDTO.getAddress())){
                        return createReceiveGiftRecord(receiveGiftDTO, relative.getRelativeId());
                    }else {
                        Relative relative1 = new Relative();
                        relative1.setUserId(receiveGiftDTO.getUserId());
                        relative1.setName(receiveGiftDTO.getReceiveGiftName());
                        relative1.setAddress(receiveGiftDTO.getAddress());
                        relative1.setRemark("");
                        relativeService.save(relative1);
                        return createReceiveGiftRecord(receiveGiftDTO, relative1.getRelativeId());
                    }
                }
            }
        }



        // 1.1 list<1(亲朋表没有同名用户) =》创建用户+添加收礼记录
        // 1.2 list=1(亲朋表有一个用户 没有填写地址) 返回有存在多个用户，请输入地址
        // 1.3 list=1(亲朋表有一个用户 有填写地址) 添加收礼记录
        // 1.3 list>1(亲朋表有多个用户 没有填写地址) 返回有存在多个用户，请输入地址
        // 1.4 list>1(亲朋表有多个用户 有填写地址 地址一样) =》创建收礼记录
        // 1.5 list>1(亲朋表有多个用户 有填写地址 地址不一样) =》创建新用户+添加收礼记录
      return Result.success();
    }

    private Result createReceiveGiftRecord(ReceiveGiftDTO receiveGiftDTO, Integer relativeId) {
        ReceiveGift receiveGift = new ReceiveGift();
        receiveGift.setBookId(receiveGiftDTO.getBookId());
        receiveGift.setUserId(receiveGiftDTO.getUserId());
        receiveGift.setRelativeId(relativeId);
        receiveGift.setGiftName(receiveGiftDTO.getGiftName());
        receiveGift.setGiftMoney(receiveGiftDTO.getGiftMoney());
        receiveGift.setAddress(receiveGiftDTO.getAddress());
        receiveGift.setRemark(receiveGiftDTO.getRemark());
        save(receiveGift);
        return Result.success();
    }
}




